package fun.abbas.myapplication;

import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.sercurity.RiskResult;

import org.w3c.dom.Text;

/**
 * @author Abbas
 * @createTime 2025/5/27 17:55
 * @desc TODO
 */
public class MyAdapter2 extends ListAdapter<RiskResult<?>, MyAdapter2.MyViewHolder> {

    protected MyAdapter2() {
        super(new DiffUtil.ItemCallback<RiskResult<?>>() {
            @Override
            public boolean areItemsTheSame(@NonNull RiskResult<?> oldItem, @NonNull RiskResult<?> newItem) {
                return oldItem.getType() == newItem.getType();
            }

            @Override
            public boolean areContentsTheSame(@NonNull RiskResult<?> oldItem, @NonNull RiskResult<?> newItem) {
                return oldItem.isInRisk() == newItem.isInRisk();
            }
        });
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item2, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        RiskResult<?> item = getItem(position);

        holder.type.setText(item.getType().getRiskType());

        if (item.isInRisk()) {
            holder.content.setVisibility(View.VISIBLE);
            holder.im.setImageResource(R.drawable.baseline_error_24);
            if (item.getData() != null) {
                holder.content.setText(item.getData().toString());
            } else {
                holder.content.setText("未携带数据");
            }
        } else {
            holder.im.setImageResource(R.drawable.baseline_check_circle_24);
            holder.content.setVisibility(View.GONE);
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView im;
        private TextView type;
        private TextView content;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            im = itemView.findViewById(R.id.im);
            type = itemView.findViewById(R.id.type);
            content = itemView.findViewById(R.id.content);
        }
    }
}
